package com.webserver.core;

import com.webserver.annotation.RequestMapping;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 该类维护所有请求与对应的业务处理类的关系
 */
public class HandlerMapping {
    private static Map<String,MethodMapping> mapping = new HashMap<>();


    static {
        initMapping();
    }

    private static void initMapping() {
        try {
            File HandlerMapping = new File(
                    HandlerMapping.class.getClassLoader().getResource(
                            "./com/webserver/controller").toURI()
                    );
            File[] files = HandlerMapping.listFiles(f->f.getName().endsWith(".class"));
            for (File file : files){
                String className = file.getName().substring(0,file.getName().indexOf("."));
                Class cls = Class.forName("com.webserver.controller."+className);
                if (cls.isAnnotationPresent(RequestMapping.class)){
                    Object controller = cls.newInstance();
                    Method[] methods = cls.getDeclaredMethods();
                    for (Method method : methods){
                        if (method.isAnnotationPresent(RequestMapping.class)){
                            RequestMapping rm = method.getAnnotation(RequestMapping.class);
                            MethodMapping methodMapping = new MethodMapping(controller,method);
                            String value = rm.value();
                            mapping.put(value,methodMapping);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static MethodMapping getMethod(String path){
        return mapping.get(path);
    }


    public static class MethodMapping{
        private Object controller;
        private Method method;

        public MethodMapping(Object controller, Method method) {
            this.controller = controller;
            this.method = method;
        }



        public Object getController() {
            return controller;
        }

        public Method getMethod() {
            return method;
        }

        @Override
        public String toString() {
            return "MethodMapping{" +
                    "controller=" + controller +
                    ", method=" + method +
                    '}';
        }
    }

    public static void main(String[] args) {
        try {
            File HandlerMapping = new File(
                    HandlerMapping.class.getClassLoader().getResource(
                            "./com/webserver/controller").toURI()
            );
            File[] files = HandlerMapping.listFiles(f->f.getName().endsWith(".class"));
            for (File file : files){
                String className = file.getName().substring(0,file.getName().indexOf("."));
                Class cls = Class.forName("com.webserver.controller."+className);
                if (cls.isAnnotationPresent(RequestMapping.class)){
                    Object controller = cls.newInstance();
                    Method[] methods = cls.getDeclaredMethods();
                    for (Method method : methods){
                        if (method.isAnnotationPresent(RequestMapping.class)){
                            RequestMapping rm = method.getAnnotation(RequestMapping.class);
                            MethodMapping methodMapping = new MethodMapping(controller,method);
                            String value = rm.value();
                            mapping.put(value,methodMapping);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

}
